<?php

namespace App\Store\Controllers\Financial\Business;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Services\Store\Common as StoreCommon;

use App\Common\Services\CommonExcelExport;
use App\Store\Models\Account\Account;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Order\OrderCommissionLog;
use App\Store\Models\Order\OrderRecoveryGoods;
use App\Store\Models\Order\OrderSalesGoods;
use App\Store\Models\Order\OrderSalesRecoveryGoods;
use Maatwebsite\Excel\Facades\Excel;
/**
 * 销售提成
 */
class SalesCommissionController extends Controller
{

    public function getlist(Request $request){
        $store_id=intval($request->store_id);
        if($store_id<=0){
            return response()->json(['code' => '001', 'msg' => '请选择门店']);
        }
        $list=OrderCommissionLog::getList($request);

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        if ($accountinfo['type'] == 11) {
            $training = StoreCommon::gettraining($request);
            if ($training['training_min'] == 0 && $training['training_max'] == 0) {
                return response()->json(['code' => '001', 'msg' => '培训账号，请先设置公司培训上下限']);
            }
        }

        if ($accountinfo['type'] == 11) {
            $sales_where = [
                ['total_money', '>', $training['training_min']],
                ['total_money', '<', $training['training_max']],
            ];
            $sales_recovery_where = [
                ['money', '>', $training['training_min']],
                ['money', '<', $training['training_max']],
            ];
            $recovery_where = [
                ['money', '>', $training['training_min']],
                ['money', '<', $training['training_max']],
            ];
        } else {
            $sales_where = [];
            $sales_recovery_where = [];
            $recovery_where = [];
        }
        foreach($list['list'] as &$item){
            in_array($item['type'],[1,2,3,4]) && $item['subinfo']= OrderSalesGoods::where(['id'=>$item['sub_id']] + $sales_where)->first()->toArray();
            $item['type']==5 && $item['subinfo']= OrderSalesRecoveryGoods::where(['id'=>$item['sub_id']] + $sales_recovery_where)->first()->toArray();
            $item['type']==6 && $item['subinfo']= OrderRecoveryGoods::where(['id'=>$item['sub_id']] + $recovery_where)->first()->toArray();
        }
        return response()->json(['code' => '000', 'msg' => '获取成功','data'=>$list]);
    }

    public function getsummarydata(Request $request){
        $store_id=intval($request->store_id);
        if($store_id<=0){
            return response()->json(['code' => '001', 'msg' => '请选择门店']);
        }
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);

        $storeinfo = SiteStore::where(['id' => $store_id, 'cp_id' => $cp_id])->first();
        if (!$storeinfo) {
            return response()->json(['code' => '001', 'msg' => '无效门店参数']);
        }
        //'1按件提成、2金价优惠提成、3工费优惠提成、4金价工费提成、5换货提成、6纯回收提成',
        $type_arr=[1,2,3,4,5,6];
        $where=[];
        $where[]=['store_id',$store_id];
        $where[]=['cp_id',$cp_id];
        $type = intval($request->type);
        if ($type>0 && in_array($type,$type_arr)) {
            $where[]=['type',$type];
        }
        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $where[]=['create_at', '>=', $start_time];
            $where[]=['create_at', '<=', $end_time];
        }
        $accountquery=Account::where([['store_ids',$store_id],['cp_id',$cp_id],['status',1]])->whereIn('type',[1,2]);
        $account_id=intval($request->account_id);
        if($account_id>0){
            $accountquery->where('id',$account_id);
        }
        $accountarr=$accountquery ->get()->toArray();

        $list=[];
        foreach($accountarr as $val){
            $item_v=[
                'store_name'=>$storeinfo->store_name,
                'account_real_name'=>$val['real_name'],
                'account_id'=>$val['id']
            ];
            if(isset($where['type']) && $where['type']>0){//查单类型记录
                $type_v=$where['type'];
                $item=$item_v;
                $item['type']=$type_v;
                $commission1=OrderCommissionLog::where(['type'=>$type_v,'shopping_account_id1'=>$val['id'],'red_status'=>1,'audit_status'=>2])->where($where)->sum('commission1');
                $commission2=OrderCommissionLog::where(['type'=>$type_v,'shopping_account_id2'=>$val['id'],'red_status'=>1,'audit_status'=>2])->where($where)->sum('commission2');
                $commission=$commission1+$commission2;
                $item['commission']=$commission;
                if($commission>0){
                    $list[]=$item;
                }
            }else{
                foreach($type_arr as $type_v){
                    $item=$item_v;
                    $item['type']=$type_v;
                    $commission1=OrderCommissionLog::where(['type'=>$type_v,'shopping_account_id1'=>$val['id'],'red_status'=>1,'audit_status'=>2])->where($where)->sum('commission1');
                    $commission2=OrderCommissionLog::where(['type'=>$type_v,'shopping_account_id2'=>$val['id'],'red_status'=>1,'audit_status'=>2])->where($where)->sum('commission2');
                    $commission=$commission1+$commission2;
                    $item['commission']=$commission;
                    if($commission>0){
                        $list[]=$item;
                    }
                }
            }
        }

        $commission_total=array_sum(array_column($list,'commission'));
        return response()->json(['code' => '000', 'msg' => '获取成功','data'=>['list'=>$list,'commission_total'=>$commission_total]]);
    }

}
